﻿#include "JkLog.h"
#include "global.h"
#include <sysinfoapi.h>
#include <fstream>

JkLog::JkLog()
{
	m_loglevel = INFOLOG;
}
JkLog::JkLog(int logLevel, string filename)
{
	m_loglevel = logLevel;
	m_logfilename = filename;
}

JkLog::~JkLog()
{

}

void JkLog::setLevel(int logLevel)
{
	m_loglevel = logLevel;
}

void JkLog::writelog(const char* data, string level)
{
	std::string sdata = data;
	sdata = sdata.substr(0, 1024 * 99);
	ReplaceSensitiveText(sdata);
	//日志按日区分
	try
	{
		SYSTEMTIME st = {};
		GetLocalTime(&st);

		char logtmp[255] = {};
		sprintf_s(logtmp, "%04d-%02d-%02d-%s", st.wYear, st.wMonth, st.wDay, m_logfilename.c_str());

		// unicode_by_sx
		std::wstring logfile = L"\\log\\" + g_tool.UTF8ToUnicode(logtmp);
		logfile = g_tool.GetCurPathW(logfile.c_str());

		//std::string logfile = "\\log\\" + (std::string)logtmp;
		//logfile = browser_GetCurPath(logfile);


		char tmp[1024 * 100] = {};
		sprintf_s(tmp, "[%04d-%02d-%02d %02d:%02d:%02d:%03d] %s--%s\r\n",
			st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, level.c_str(), sdata.c_str());

		std::fstream file;
		file.open(logfile.c_str(), std::ios_base::app);
		if (!file.is_open()) {
			return;
		}
		file.write(tmp, strlen(tmp));
		file.close();
	}
	catch (...) {

	}

}
void JkLog::writelog(const std::wstring data, string level)
{
	writelog(g_tool.UnicodeToUtf8(data.c_str()).c_str(), level);
}
void JkLog::DebugLog(const char* data)
{
	if (m_loglevel >= DEBUGlOG)
	{
		writelog(data, JKDEBUG);
	}
}

void JkLog::DebugLog(const std::wstring data)
{
	if (m_loglevel >= DEBUGlOG)
	{
		writelog(g_tool.UnicodeToUtf8(data.c_str()).c_str(), JKDEBUG);
	}
}

void JkLog::DebugLogW(const std::wstring data)
{
	if (m_loglevel >= DEBUGlOG)
	{
		writelog(g_tool.UnicodeToUtf8(data.c_str()).c_str(), JKDEBUG);
	}
}

void JkLog::InfoLog(const char* data)
{
	if (m_loglevel >= INFOLOG)
	{
		writelog(data, JKINFO);
	}
}

void JkLog::InfoLog(const std::wstring data)
{
	if (m_loglevel >= INFOLOG)
	{
		writelog(g_tool.UnicodeToUtf8(data.c_str()).c_str(), JKINFO);
	}
}

void JkLog::InfoLogW(const std::wstring data)
{
	if (m_loglevel >= INFOLOG)
	{
		writelog(g_tool.UnicodeToUtf8(data.c_str()).c_str(), JKINFO);
	}
}

void JkLog::WarnLog(const char* data)
{
	if (m_loglevel >= WARNLOG)
	{
		writelog(data, JKWARN);
	}
}

void JkLog::WarnLog(const std::wstring data)
{
	if (m_loglevel >= WARNLOG)
	{
		writelog(g_tool.UnicodeToUtf8(data.c_str()).c_str(), JKWARN);
	}
}

void JkLog::WarnLogW(const std::wstring data)
{
	if (m_loglevel >= WARNLOG)
	{
		writelog(g_tool.UnicodeToUtf8(data.c_str()).c_str(), JKWARN);
	}
}

void JkLog::ErrorLog(const char* data)
{
	if (m_loglevel >= ERRORLOG)
	{
		writelog(data, JKERROR);
	}
}

void JkLog::ErrorLog(const std::wstring data)
{
	if (m_loglevel >= ERRORLOG)
	{
		writelog(g_tool.UnicodeToUtf8(data.c_str()).c_str(), JKERROR);
	}
}

void JkLog::ErrorLogW(const std::wstring data)
{
	if (m_loglevel >= ERRORLOG)
	{
		writelog(g_tool.UnicodeToUtf8(data.c_str()).c_str(), JKERROR);
	}
}
void JkLog::ReplaceSensitiveText(std::string& data)
{
	data = g_replace.replace_all(data, "app_jackyun_manager", "***JACKYUN***");
    data = g_replace.replace_all(data, "wdgj_j_client", "***WDGJ***");
}